/*
 * Copyright (C) 2009 Armence
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * This is an application that is meant to demonstrate how to use the
 * Text to Speech engine provided with Android. Learn from it, have fun
 * with it, but don't expect more than that. Or if you do, don't blame
 * me if you get disappointed.
 * 
 * The app has 3 widgets: a TextView, an EditText and a Button
 * You write something in the EditText and click or tap the Button
 * and whatever you wrote gets spoken by your phone/emulator
 * 
 *  Now from a more technical discussion of what is happening
 *  So first, onCreate is called by Android and it creates the UI
 *  Then the TextToSpeech engine is created
 *  However, you cannot process any text until the TTS engine is initiated
 *  So you wait until that is done, then you set a flag to note that
 *  Then, if the user clicks, you  speack the text
 *  and finally you shutdown the engine to return it to system resource
 */

package com.example.droidtts;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

// implements OnInitListener is there is order to let the TTS engine that this class
// contains the onInit method
public class DroidSpeaks extends Activity implements OnClickListener, OnInitListener{
	private TextToSpeech tts;
	boolean ttsInit = false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button speak = (Button)findViewById(R.id.speak);
        speak.setOnClickListener(this);
        speak.setClickable(false);
        
    	// This starts up the TextToSpeech engine
    	// The first parameter is the context in which it is operating
    	// The second is the class which contains onInit        
        this.tts = new TextToSpeech(this, this);
    }
    
    public void onClick(View v) {
    	if(ttsInit) {
    		// Get the text from the text box
        	EditText input = (EditText)findViewById(R.id.txtEntry);
        	String s = input.getText().toString();
    	    // This speaks the text
    	    this.tts.speak(s, TextToSpeech.QUEUE_ADD, null);
    	}
    }
    
    // When the TTS engine is initialized, this method is called
    public void onInit(int status) {
    	this.ttsInit = true;
    	Button speak = (Button)findViewById(R.id.speak);
    	speak.setClickable(true);
    }
    
    public void onDestroy() {
    	// This shuts down the TTS engine
    	tts.shutdown();
    }
}